// http://code.google.com/p/wave-as-client
// @author sol wu

var flashObj = null;

function init() {
	if (wave && wave.isInWaveContainer()) {
		wave.setStateCallback(stateCallback);
		wave.setParticipantCallback(participantCallback);
	}
}

/**
 * setup the callback function.
 */
function setStateCallback() {
	if (wave && wave.isInWaveContainer()) {
		wave.setStateCallback(stateCallback);
	}
}

/**
 * setup the callback function.
 */
function setParticipantCallback() {
	if (wave && wave.isInWaveContainer()) {
		wave.setParticipantCallback(participantCallback);
	}
}

/**
 * callback function for stateCallback.  
 * 
 * translate the object to generic object and call the function on flash.  
 */
function stateCallback() {
	// todo: pass in flash object id
	// we need to pass back the entire state object each time, is there a better way?
	var stateCopy = unboxState(wave.getState());
	flashObj.externalWaveStateCallback(stateCopy);
}

/**
 * callback function for participantCallback.  
 * 
 * translate the object to generic object and call the function on flash.  
 */
function participantCallback() {
	// todo: pass in flash object id
	// we need to pass back the entire participant object each time, is there a better way?
	var participantsCopy = unboxParticipants(wave.getParticipants());
	flashObj.externalWaveParticipantCallback(participantsCopy);
}

/**
 * translate wave.State to generic object, so that the generic object can be passed to flex.  
 */
function unboxState(state) {
	var obj = {};  
	var keys = state.getKeys();
	for (var i=0; i < keys.length; ++i) {
		var key = keys[i];
		obj[key] = state.get(key);
	}
	return obj;
}

/**
 * translate array of wave.Participant objects to generic objects, so that the generic object can be passed to flex.     
 */
function unboxParticipants(participants) {
	var objs = [];
	for (var i = 0; i < participants.length; ++i) {
		var obj = unboxParticipant(participants[i]);
		objs.push(obj);
	}
	return objs;
}

/**
 * translate wave.Participant to generic object, so that the generic object can be passed to flex.     
 */
function unboxParticipant(participant) {
	var obj = {};
	obj.id = participant.getId();
	obj.displayName = participant.getDisplayName();
	obj.thumbnailUrl = participant.getThumbnailUrl();
	return obj;
}